import {
    Component,
    ViewChild,
    Injector,
    Output,
    EventEmitter,
    ElementRef,
    AfterViewInit,
    ViewEncapsulation,
    Inject,
    forwardRef
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    SimpleTechDocDto,
    TechDocDto,
    TechDocServiceProxy
} from '@shared/service-proxies/service-proxies';
import { PipelineNodesServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ActBaseModalComponent } from '@app/pipeline/shared/act-base.component';

@Component({
    selector: 'gy1',
    templateUrl: './gy1.component.html',
    styleUrls: ['./gy1.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class GY1ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();

    techDocs = new Array<SimpleTechDocDto>();
    selectedTechDoc = new SimpleTechDocDto();
    fiter = '';
    filterText = '';
    techDocView = new TechDocDto();
    oldTechDocView = new TechDocDto();
    cols: any[];
    selectedColumns: any[];
    //actBase: ActBaseModalComponent;
    drawingCode = '';
    constructor(
        injector: Injector,
        private _techDocServiceProxy: TechDocServiceProxy,
        private _taskServiceProxy: PipelineNodesServiceProxy // @Inject(forwardRef(() => ActBaseModalComponent)) actBase: ActBaseModalComponent
    ) {
        super(injector);
        //this.actBase = actBase;
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
        this.filterText = pipe.drawingCode;
        this._taskServiceProxy.getTechDocCode(pipe.id).subscribe(result => {
            if ( result != null) {
            this._techDocServiceProxy
                .getTechDocForView(result)
                .subscribe(result1 => {
                    this.oldTechDocView = result1;
                    this.techDocView = result1;
                    // console.log(result1);
                    if (result1.codeId) {
                        this.selectedTechDoc = this.techDocs.filter(el => {
                            return el.codeId === result1.codeId;
                        })[0];
                    } else {
                        this.selectedTechDoc = new SimpleTechDocDto();
                    }
                    this.rowSelect(event);
                });
            }
        });
        this.getCharges();
    }
    getCharges() {
        if ( this.filterText === '') {
            this.filterText = this.pipe.drawingCode;
        }
        this._techDocServiceProxy
        .getSimpleTechDocsByRef(this.filterText, '58')
        .subscribe(result => {
            this.techDocs = result.items;
        });
    }
    // screenlist(drawingCode): void {
    //     this._techDocServiceProxy
    //         .getSimpleTechDocsByRef(drawingCode, '58')
    //         .subscribe(result => {
    //             console.log(result);
    //             this.techDocs = result.items;
    //         });
    // }

    rowSelect(event: any) {
        let rowData = event.data;
        this._techDocServiceProxy
            .getTechDocForView(rowData.codeId)
            .subscribe(result => {
                this.techDocView = result;
                console.log(result);
            });
    }

    save(): void {
        this.saving = true;

        this._taskServiceProxy
            .boundTechDoc(this.pipe.id, this.selectedTechDoc.codeId)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(result => {
                this.notify.success('绑定成功');
            });
    }
}
